package fun.wlfj.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import fun.wlfj.dao.IUserDao;
import fun.wlfj.dbc.IConnectionManager;
import fun.wlfj.dbc.MySQLConnection;
import fun.wlfj.vo.User;

public class UserDaoImpl extends AbsDaoImpl implements IUserDao {

	public UserDaoImpl(Connection conn) {
		super(conn);
	}

	@Override
	public User Login(User u) {
		try {
			PreparedStatement pstsm = conn.prepareStatement("select * from User where UserName = ? AND Password = ?;");
			pstsm.setString(1, u.getUserName());
			pstsm.setNString(2, u.getPassword());
			ResultSet rs = pstsm.executeQuery();
			if(rs.next()) {
				u.setUserID(rs.getInt(1));
				u.setUserSex(rs.getString(4));
				u.setUserTel(rs.getString(5));
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return u;
	}

	@Override
	public boolean Regist(User u) {
		boolean isOK = false;
		try {
			PreparedStatement pstsm = conn.prepareStatement("insert into User(UserName, Password, UserSex, UserTel) values(?, ?, ?, ?);");
			pstsm.setString(1, u.getUserName());
			pstsm.setNString(2, u.getPassword());
			pstsm.setNString(3, u.getUserSex());
			pstsm.setNString(4, u.getUserTel());
			if(pstsm.executeUpdate() == 1) {
				isOK = true;
			}else {
				isOK = false;
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return isOK;
	}
	
	public static void main(String[] args) {
		// 下面我们来测试注册功能是否好用呢？
		IConnectionManager manager = new MySQLConnection();
		IUserDao test = new UserDaoImpl(manager.getConn());
		User u1 = test.Login(new User("yves", "1234"));
		System.out.println("登录测试：" + u1);
		test.Regist(new User("Wong", "男", "17503449316", "4321"));
	}

}
